package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.CustomerDao;
import com.atguigu.jxc.entity.Customer;
import com.atguigu.jxc.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    CustomerDao customerDao;

    @Override
    public Map<String, Object> list(Integer page, Integer rows, String customerName ) {
        Map<String,Object> map = new HashMap<>();

        page = page == 0 ? 1 : page;
        int offSet = (page - 1) * rows;
        List<Customer> customers = customerDao.getCustomerList(offSet, rows, customerName);

        map.put("total", customerDao.getCustomerCount(customerName));

        map.put("rows", customers);
        return map;
    }

    @Override
    public void addSupplier(Customer customer, Integer customerId) {
        if (customerId != null){

            customerDao.updateCustomerById(customer);

        }else {

            customerDao.addCustomer(customer);

        }
    }

    @Override
    public void deleteCustomer(String ids) {

            String[] arrays = ids.split(",");

            customerDao.deleteCustomer(arrays);


    }
}
